var wid, uid, pic, dtype = 0;
document.addEventListener('plusready',function(){
	wid = plus.webview.currentWebview().id;
	uid = plus.storage.getItem('uid');
	var applytime = plus.storage.getItem('applytime');//申请时间
	var status = plus.storage.getItem('status');//用认证状态
	var authtime = plus.storage.getItem('authtime');//认证成功时间
	console.log(status);
	if(status == 2 || status == 1){
		plus.nativeUI.closeWaiting();
//		$('.renzheng').hide();
//		$('.renzhengzhong').show();
//		$('.applyInfo').text('您好！您的相关资料正在认证中，敬请耐心等待。');
//		$('.applytime').text(applytime);
//		var info = '<div class="progress-li mui-table-view background-ddd">'+
//						'<li>'+
//							'<span>恭喜您已通过我们的认证!</span>'+
//							'<span class="mui-h5">'+authtime+'</span>'+
//							'<div class="ring"></div>'+
//						'</li>'+
//					'</div>';
//		$('.ccc').html(info);
//	}else if(status == 1){
		$('.renzheng').show();
		$('.renzhengzhong').hide();
		if(plus.storage.getItem('license') != 'null')$('.license').attr('src',webRoot+plus.storage.getItem('license'));
		if(plus.storage.getItem('front') != 'null')$('.front').attr('src',webRoot+plus.storage.getItem('front'));
		if(plus.storage.getItem('tails') != 'null')$('.tails').attr('src',webRoot+plus.storage.getItem('tails'));
		if(plus.storage.getItem('certificate') != 'null'){
			var arr = splitArray(plus.storage.getItem('certificate'));
			$.each(arr, function(k, v){
				$('.certificate').parent().prepend('<img src="'+webRoot+v+'"/>');
			})
			if(status == 2){
				$('.over').hide();
				//点击图片放大效果
				$('img[class!=certificate]').attr('data-preview-src','');
				$('img[class!=certificate]').attr('data-preview-group','1');
			}
		}
		if(plus.storage.getItem('photo') != 'null')$('.photo').attr('src',webRoot+plus.storage.getItem('photo'));
		if(plus.storage.getItem('experience'))$('.experience').val(plus.storage.getItem('experience'));
		if(plus.storage.getItem('chiefid'))$('.hidid').val(plus.storage.getItem('chiefid'));
		if(plus.storage.getItem('chief'))$('.font-15').text(plus.storage.getItem('chief'));
		var doctortype = plus.storage.getItem('dtype');
		console.log(doctortype);
		if(status == 1){
			renzhengzhong();
			$('.submit').show();
			$('input[name=radio1]').each(function(){
				if($(this).val() == doctortype){
					$(this).attr('checked', true);
				}
			})
		}else{
			$('.certificate').remove();
			$('textarea').attr('disabled', true);
			$('.font-15').removeClass('mui-navigate-right');
			$('.type1').hide();
			$('.type2').show();
			$('.type2 span').text(doctortype == 0 ? '中医' : '西医');
		}
	}else{
		$('.renzheng').show();
		$('.renzhengzhong').hide(); 
		renzhengzhong();
		$('.submit').show();
	}
	//选择医生类型
	$("input[name='radio1']").click(function(){
    	dtype = $(this).attr("value");
    });
},false)
window.addEventListener('change',function(event){
	var studioid = event.detail.studioid;
	var studioname = event.detail.studioname;
	console.log(studioid+" "+studioname);
	$('.hidid').val(studioid);
	$('.font-15').text(studioname);
})
var _this;
function renzhengzhong(){
	plus.nativeUI.closeWaiting();
	//选择工作室
	$('.changStudio').on('click',function(){
		var tid = dtype == 0 ? 91 : 92;
		console.log(wid+" "+tid);
		goNewPage('me-rz-authentication-list.html',{spage:wid, tid:tid});
	})
	//审核信息->上传图片
    $('.uploadPic').on('click',function(){
		_this = $(this);
		mui('#sheet1').popover('toggle');
    })
    $('.paizhao').on('tap', function(){
    	var cmr = plus.camera.getCamera(); 
		cmr.captureImage( function(path){
			path = plus.io.convertLocalFileSystemURL(path);
			appendPic('file://'+path);
		}, function(err){}, {index:1} );
    })
    $('.xiangce').on('tap', function(){
    	plus.gallery.pick( function(path){
		 	appendPic(path);
		}, function(error){}, {} );
    })
	var files = {};
	// 添加照片
	function appendPic(p) {
		files = {};
		var newPath = p.replace(/\./g, new Date().getTime() + '.');
		plus.zip.compressImage({
			src: p,
			dst: newPath,
			overwrite: true,
			quality: 20
		},
		function() { //成功回调
			if(_this.attr('cname') != "certificate"){	//医生资格证
				_this.attr('src',newPath);
			}else{
//				_this.before('<img src="'+webRoot+t.responseText+'"/>');
			}
			files.name = "uploadkey";
			files.path = newPath;
			upload();
		},
		function(error) {
			toast('压缩图片失败');
		});
		
	}
	var cate = '';//医生资格证
	//创建对像
	function upload(){ 
		var wt = plus.nativeUI.showWaiting('正在处理图片');  
		var  task = plus.uploader.createUpload(apiRoot+'?m=Home&c=Member&a=uploadOne',{ method:"POST"},function ( t, status ) {
			// 上传完成     
			if ( status == 200 && t.responseText.indexOf("/uploads") != -1) { 
				console.log(t.responseText);
				if(_this.attr('cname') != "certificate"){
					plus.storage.setItem(_this.attr('cname'),t.responseText+"");
				}else{
					_this.before('<img src="'+webRoot+t.responseText+'"/>');
				}
//				console.log(plus.storage.getItem(_this.attr('cname')));
				wt.close();
			} else {
				toast( "上传失败请尝试重新上传");
				wt.close();
			}  
		});
		task.addFile(files.path,{key:files.name});
		task.start();
	}
	//提交资料审核信息
	$('.submit').on('click',function(){
		var experience = $('.experience').val();
		var studioid = $('.hidid').val();
		var studioname = $('.font-15').text() == '请选择医疗组' ? '': $('.font-15').text();
		var license = plus.storage.getItem('license');
		var front = plus.storage.getItem('front');
		var tails = plus.storage.getItem('tails');
		$('.certificate').siblings('img').each(function(){
			var _this = $(this);
			var p = _this.attr('src');
			console.log(p);
			p = p.replace(webRoot, '');
			cate += '|-|' + p + '|,|';
		})
		if(cate != ''){
			cate = cate.substr(3);	//从第四个字符开始
		}
		var certificate = cate;
		console.log(license);
		console.log(front);
		console.log(tails+" "+certificate);
		console.log(photo+" "+experience);  
		plus.storage.setItem('certificate', certificate);
		var photo = plus.storage.getItem('photo');
		if(!license || license.indexOf("/uploads") == -1){
			toast('医生执照上传失败请重新上传');return;
		}else if(!front || front.indexOf("/uploads") == -1){
			toast('身份证正面照上传失败请重新上传');return;
		}else if(!tails || tails.indexOf("/uploads") == -1){
			toast('身份证背面照上传失败请重新上传');return;
		}else if(!certificate || certificate.indexOf("/uploads") == -1){
			toast('医生资格证照上传失败请重新上传');return;
		}else if(!photo || photo.indexOf("/uploads") == -1){
			toast('个人照片上传失败请重新上传');return;
		}
		if(!license|| !front|| !tails|| !certificate|| !photo || !experience){
			toast('请完善信息');return;
		}
		showWating();
		$.ajax({
			type:"post",
			url:apiRoot+"?m=home&c=member&a=submitData",
			data :{
				uid :uid,
				dtype :dtype,
				license:license,
				front:front,
				tails:tails,
				certificate:certificate,
				photo:photo,
				experience :experience,
				chiefid :studioid,
				chief :studioname,
				d :1
			},
			dataType :'json',
			success :function(data){
				console.log(JSON.stringify(data));
					plus.nativeUI.closeWaiting();
				if(data > 0){
					plus.storage.setItem('experience',experience+'');
					plus.storage.setItem('chiefid',studioid+'');
					plus.storage.setItem('chief',studioname+'');
					plus.storage.setItem('dtype',dtype+'');//医生类型
					toast('资料提交成功');
					$('.renzheng').hide();
					$('.renzhengzhong').show();
					renzheng();
				}
			},
			error : function(e){
				console.log(JSON.stringify(e));
			}
		});	
	})
}
function renzheng(){
//	console.log(apiRoot+"?m=home&c=member&a=authStatus&uid="+uid);
	$.ajax({
		type :"get",
		url :apiRoot+"?m=home&c=member&a=authStatus",
		data :{
			uid :uid
		},
		dataType : 'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(data));
			if(data.status >= 1){
				$('.applyInfo').text('您好！您的相关资料正在认证中，敬请耐心等待。');
				$('.applytime').text(data.applytime);
				plus.storage.setItem('status',data.status+'');//认证状态
				plus.storage.setItem('applytime',data.applytime+'');//申请时间
				if(data.status == 2){
					var info = '<div class="progress-li mui-table-view background-ddd">'+
									'<li>'+
										'<span>恭喜您已通过我们的认证!</span>'+
										'<span class="mui-h5">'+data.authtime+'</span>'+
										'<div class="ring"></div>'+
									'</li>'+
								'</div>';
					console.log(info);
					$('.ccc').html(info);
					plus.storage.setItem('authtime',data.authtime+'');//认证成功时间
				}
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
